extends CharacterBody2D

class_name Player
@onready var raycast = $RayCast2D#角色向下发出射线只能与第三层碰撞，而第三层是单向平台，可以实现跳下平台
@onready var sprite_2d = $Sprite2D
@onready var animation_tree = $AnimationTree
@onready var animation_state_machine : AnimationNodeStateMachinePlayback = animation_tree.get("parameters/playback")
@onready var state_chart = $StateChart
@onready var water_particle = $"../WaterParticle"
@onready var HeartUi = $"../ParallaxBackground/HeartUi"
@onready var heart_label = $"../ParallaxBackground/HeartLabel"
@onready var blood_particle = $"../ParallaxBackground/BloodParticle"
@onready var gameover_screen = $"../GameoverScreen"
@onready var camera_2d = $"../ParallaxBackground/Camera2D"
@onready var can_double_jump = 0 
@onready var rebirth_markers = $"../RebirthMarkers"

@export var wall_slide_speed: float = 70
@export var JUMP_VELOCITY = -350.0

const PLATFORM_CHECK_RADIUS = 10.0  # 用于检测是否在平台上
const PLATFORM_LAYER = 1 << 2       # 平台的物理层，3 代表第 4 层（从 0 开始）

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction and not is_on_floor():
		velocity.x = direction * GR.SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, GR.SPEED)
	# Add the gravity.
	if is_on_floor():
		GR.SPEED=170.0
		state_chart.send_event("grounded")
	else:
		if GR.is_rebirth:
			velocity.y=0
			velocity.x=0
		else:
			state_chart.send_event("airborne")
			velocity.y += gravity * delta
	if velocity.x != 0:#左右翻转
		sprite_2d.flip_h = velocity.x < 0	
	if velocity != Vector2.ZERO:
		state_chart.send_event("moving")
	else:
		state_chart.send_event("idle")
	animation_tree["parameters/Move/blend_position"] = signf(velocity.y)#负数返回负一，整数返回1，零返回0


	
	move_and_slide()

func _input(event: InputEvent):#跳下平台
	if event.is_action_pressed("ui_down") and raycast.is_colliding():
		position.y += 5




##检查跳跃按键输入
func check_jump_pressed():
	if Input.is_action_just_pressed("ui_accept"):
		state_chart.send_event("Jump")
		velocity.y = JUMP_VELOCITY

func _on_grounded_state_physics_processing(delta) -> void:
	#await get_tree().create_timer(0.2).timeout#播放一小段蓄力动画
	state_chart.send_event("Jump")
	velocity.y = JUMP_VELOCITY

#二段跳
func _on_jump_state_physics_processing(delta) -> void:
	if can_double_jump:
		check_jump_pressed()



## 收集道具
func collect():
	print("玩家收集了道具")







func rebirth():
	var Background_center_x = GR.screen_position.x
	var viewport_size_x = get_viewport().size.x
	var rebirth_position = Vector2.ZERO
	for marker in rebirth_markers.get_children():#找到当前视口内的复活点
		if GR.screen_position.x<=marker.position.x and marker.position.x<=GR.screen_position.x+viewport_size_x:
			rebirth_position=marker.position
			break
	position=rebirth_position
	state_chart.send_event("hitted")
	GR.is_rebirth=1
	await get_tree().create_timer(0.65).timeout#复活
	GR.is_rebirth=0
	
	
	
signal gameover	
func _gameover():

	HeartUi.play("broken")#播放心碎动画
	water_particle.position=position#设置水花位置
	water_particle.emitting=true#播放水花粒子动画
	blood_particle.emitting=true#播放心碎粒子动画
	camera_2d.shake_camera()
	heart_label.text="x%d" %GR.life
	if GR.life==0:
		gameover_screen.position=GR.screen_position#把游戏结束屏幕移动到当前视口
		gameover_screen.show_game_over()
		await get_tree().create_timer(2.0).timeout#游戏结束屏幕显示
		gameover.emit()
	else:
		rebirth()

		











